#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gtk
import gobject
from gettext import gettext as _

class AgendaWindow (gtk.Window):
#
# hide the window if it looses focus to another window
# but if a popup is activated, the window is also hidden 
#	this is not wirking as expected
#	def focus_out_cb(self,widget,event):
#		"""hide the window if it looses focus to another application"""
#		print "focus_oud_cb"
#		print widget
#		print "---"
#		if(not self.popup_menu.is_focus()):
#			self.toggle_window_radio_btn.set_active(False)
#		return True		
	"""
	Window aligning itself to a status Icon
	"""
	def __init__(self, statusIcon, type=gtk.WINDOW_TOPLEVEL):
		"""
		Call AgendaWindow.update_position () to position the window.
		"""
		gtk.Window.__init__(self, type)
		#self.set_decorated (False)
		
		# Skip the taskbar, and the pager, stick and stay on top
		self.stick()
		#self.set_keep_above(True)
		self.set_skip_pager_hint(True)
		self.set_skip_taskbar_hint(True)
		self.set_border_width(0)
		self.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)

		#self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_MENU)
				
		
		self.statusIcon = statusIcon
		
		self.realize_status = None
		
		self.connect ("realize", lambda win : self.__register_realize ())
		self.connect ("delete-event", self.on_delete_event)

		try:
			(screen, area, alignment) = self.statusIcon.get_geometry()
			self.support_alignment = True
		except:
			self.support_alignment = False
			self.set_decorated(True)
			self.set_border_width(0)
			
		self.set_title(_("Agenda"))
		
			
	def on_delete_event(self, window, event):
		# Since all users of this class expect it to remain open and alive
		# We must catch deletion events (for example alt-f4) and just hide the window
		self.hide()
		return True

	def show_window(self):
		
		if(not self.get_property("visible")):
			self.update_position()
			self.show()
			return True
		else:
			if(not self.is_active()):
				self.present()
				return True
			else:
				self.hide()
				return False
		return False
	def toggle_window(self):
		if self.get_property("visible"):
			self.hide()
		else:
			self.show()
	def update_position (self):
		"""
		Calculates the position and moves the window to it.
		IMPORATNT: widgetToAlignWith should be realized!
		"""
	
		# Get our own dimensions & position

		if(self.support_alignment):
			(screen, area, alignment) = self.statusIcon.get_geometry()
		
			#pos of the status_icon
			(ax, ay) = (area.x,area.y)
			#window size
			(ww, wh) = self.get_size ()
			
			(aw, ah) = (area.width,area.height)

			monitor_geom = screen.get_monitor_geometry (screen.get_monitor_at_point (ax,ay))
			x = 0
			y = 0
			
			if alignment == gtk.ORIENTATION_HORIZONTAL:
				x = ax
				y = ay
				if (y + wh > monitor_geom.y + monitor_geom.height):
					y = monitor_geom.y + monitor_geom.height - wh
				
				if (y < 0):
					y = 0
						
			elif alignment == gtk.ORIENTATION_VERTICAL:
				x = ax
				y = ay + ah
				
				if (x + ww > monitor_geom.x + monitor_geom.width):
					x = monitor_geom.x + monitor_geom.width - ww
					
				if (x < 0):
					x = 0
			self.move(x, y)
		else:
			"""The platform does  not support statusIcon.get_geometry()"""
			#TODO: somehow try to figure out where to put the window, or give it the border back so the user can move it
			self.show()
			self.move(0,0)
			#self.hide()
			pass
	def __register_realize (self):
		self.realize_status = True
		self.update_position()
		
gobject.type_register (AgendaWindow)
